package org.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import jakarta.annotation.Resource;
import org.example.dto.BlackListDTO;
import org.example.dto.WhiteListDTO;
import org.example.mapper.WhiteListMapper;
import org.example.model.BlackListDO;
import org.example.model.WhiteListDO;
import org.example.req.*;
import org.example.service.WhiteListService;
import org.example.utils.SpringBeanUtil;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class WhiteListServiceImpl implements WhiteListService {

    @Resource
    private WhiteListMapper whiteListMapper;

    @Override
        public List<WhiteListDTO> list(String result) {
        if (result == null){
            List<WhiteListDO> whiteListDOS = whiteListMapper.selectList(null);
            List<WhiteListDTO> whiteListDTOS = SpringBeanUtil.copyProperties(whiteListDOS, WhiteListDTO.class);
            return whiteListDTOS;
        }
        LambdaQueryWrapper<WhiteListDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WhiteListDO::getResult, result);
        List<WhiteListDO> whiteListDOS = whiteListMapper.selectList(queryWrapper);

        return SpringBeanUtil.copyProperties(whiteListDOS, WhiteListDTO.class);
    }

    @Override
    public WhiteListDTO findById(Long id) {
        LambdaQueryWrapper<WhiteListDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WhiteListDO::getId, id);
        WhiteListDO whiteListDO = whiteListMapper.selectOne(queryWrapper);
        return SpringBeanUtil.copyProperties(whiteListDO, WhiteListDTO.class);
    }

    @Override
    public int save(WhiteListSaveReq listSaveReq) {
        WhiteListDO whiteListDO = SpringBeanUtil.copyProperties(listSaveReq, WhiteListDO.class);

        return whiteListMapper.insert(whiteListDO);
    }

    @Override
    public int delete(WhiteListDelReq req) {
        if (req.getId() != null){
            return whiteListMapper.deleteById(req.getId());
        }
        return 0;
    }

    @Override
    public int update(WhiteListUpdateReq req) {
        WhiteListDO whiteListDO = SpringBeanUtil.copyProperties(req, WhiteListDO.class);
        LambdaQueryWrapper<WhiteListDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WhiteListDO::getId, req.getId());
        return whiteListMapper.update(whiteListDO, queryWrapper);
    }
}
